package com.tjshop.android.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TabHost;

import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.tjshop.android.R;
import com.tjshop.android.MainActivity;

/**
 * Created with IntelliJ IDEA. Author: wangjie email:tiantian.china.2@gmail.com
 * Date: 13-10-10 Time: 上午9:25
 */
public class FragmentTabAdapter extends FragmentPagerAdapter implements
		TabHost.OnTabChangeListener {
	private List<Fragment> fragments; // 一个tab页面对应一个Fragment
	private FragmentActivity fragmentActivity; // Fragment所属的Activity
	private int fragmentContentId; // Activity中所要被替换的区域的id
	private TabHost tabHost;
	private int currentTab; // 当前Tab页面索引

	public FragmentTabAdapter(MainActivity activity, TabHost tabHost,
			List<Fragment> fragments, int fragmentContentId) {
		super(activity.getSupportFragmentManager());
		this.fragments = fragments;
		this.fragmentActivity = activity;
		this.fragmentContentId = fragmentContentId;
		this.tabHost = tabHost;
		tabHost.setOnTabChangedListener(this);
	}

	/**
	 * 切换tab
	 * 
	 * @param idx
	 */
	private void showTab(int idx) {
		for (int i = 0; i < fragments.size(); i++) {
			Fragment fragment = fragments.get(i);
			FragmentTransaction ft = obtainFragmentTransaction(idx);

			if (idx == i) {
				ft.show(fragment);
			} else {
				ft.hide(fragment);
			}
			ft.commit();
		}
		currentTab = idx; // 更新目标tab为当前tab
	}

	/**
	 * 获取一个带动画的FragmentTransaction
	 * 
	 * @param index
	 * @return
	 */
	private FragmentTransaction obtainFragmentTransaction(int index) {
		FragmentTransaction ft = fragmentActivity.getSupportFragmentManager()
				.beginTransaction();
		// 设置切换动画
		if (index > currentTab) {
			ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
		} else {
			ft.setCustomAnimations(R.anim.slide_right_in,
					R.anim.slide_right_out);
		}
		return ft;
	}

	public int getCurrentTab() {
		return currentTab;
	}

	public Fragment getCurrentFragment() {
		return fragments.get(currentTab);
	}

	@Override
	public Fragment getItem(int position) {
		return fragments.get(position);
	}

	@Override
	public void onTabChanged(String tabId) {
		int position = tabHost.getCurrentTab();
		Fragment fragment = fragments.get(position);
		FragmentTransaction ft = obtainFragmentTransaction(position);

		getCurrentFragment().onPause(); // 暂停当前tab

		if (fragment.isAdded()) {
			fragment.onResume(); // 启动目标tab的onResume()
		} else {
			ft.add(fragmentContentId, fragment);
		}
		showTab(position); // 显示目标tab
		ft.commit();
	}

	@Override
	public int getCount() {
		return 4;
	}

	public static class DummyTabFactory implements TabHost.TabContentFactory {
		private final Context mContext;

		public DummyTabFactory(Context context) {
			mContext = context;
		}

		@Override
		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}
	}
}
